var devId = getQueryString("id");
var devCode = getQueryString("devCode");
var devName = decodeURI(getQueryString("devName"));
$(function () {
    $("#jqGrid").Grid({
        url: '../devcodeicc/list?devCode=' + devCode,
        colModel: [
            {label: '设备编码', name: 'devCode', index: 'DEV_CODE', width: 80, align: 'center'},
            {label: '设备名称', name: 'devName', index: 'DEV_NAME', width: 80, align: 'center'},
            // {label: '车间', name: 'workshopName', index: 'WORKSHOP_NAME', width: 80, align: 'center'},
            {label: '生产线代码', name: 'proLineId', index: 'PRO_LINE_ID', width: 80, align: 'center'},
            {label: '生产线', name: 'proLineName', index: 'PRO_LINE_NAME', width: 80, align: 'center'},
            {label: 'ICC代码', name: 'iccCode', index: 'ICC_CODE', width: 80, align: 'center'},
            {label: '备注', name: 'remark', index: 'REMARK', width: 80, align: 'center'},
            {label: 'id', name: 'id', index: 'ID', align: 'center', key: true, hidden: true},
            /*{label: '生产线ID', name: 'proLineId', index: 'PRO_LINE_ID', width: 80,align: 'center'},
            {label: '车间ID', name: 'workshopId', index: 'WORKSHOP_ID', width: 80,align: 'center'},
            {label: '设备ID', name: 'devId', index: 'DEV_ID', width: 80,align: 'center'},
			{label: '修改时间', name: 'updateDate', index: 'UPDATE_DATE', width: 80,align: 'center'},
			{label: '修改人', name: 'updateUser', index: 'UPDATE_USER', width: 80,align: 'center'},
			{label: '创建人', name: 'createUser', index: 'CREATE_USER', width: 80,align: 'center'},
			{label: '创建时间', name: 'createDate', index: 'CREATE_DATE', width: 80,align: 'center'}*/
        ]
    });
    vm.getTreeList();
    vm.getStore();
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
    callback: {
        beforeCheck: true
    }
};
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl",
            name:"complexName"
        }
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    //此处treeNode 为当前节点
    var str = '';
    str = getAllChildrenNodes(treeObj, str);
    // 加上被选择节点自己
    str = str + ',' + treeObj.deptId;
    // 去掉最前面的逗号
    var ids = str.substring(1, str.length);
    vm.q.proLineId = ids;
    vm.query();
}

// 递归，获取所有子节点
function getAllChildrenNodes(treeNode, result) {
    if (treeNode.isParent) {
        var childrenNodes = treeNode.children;
        if (childrenNodes) {
            for (var i = 0; i < childrenNodes.length; i++) {
                result += ',' + childrenNodes[i].deptId;
                result = getAllChildrenNodes(childrenNodes[i], result);
            }
        }
    }
    return result;
}
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        devCodeIcc: {},
        ruleValidate: {
            proLineName: [
                {required: true, message: '生产线不能为空', trigger: 'change'}
            ]
        },
        q: {
            devCode: '',
            proLineId: ''
        },
        treeList: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devCodeIcc = {id: '', delFlag: 1, devId: devId, devCode: devCode, devName: devName, proLineId: '', proLineName: ''};
            vm.getTree();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
            vm.getTree();
        },
        saveOrUpdate: function (event) {
            var url = vm.devCodeIcc.id == (null || '') ? "../devcodeicc/save" : "../devcodeicc/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devCodeIcc),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            vm.getInfo(ids);
            confirm('确定要删除选中的记录？', function () {
                vm.devCodeIcc.delFlag = 0;
                Ajax.request({
                    url: "../devcodeicc/update",
                    params: JSON.stringify(vm.devCodeIcc),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
            /*confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devcodeicc/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });*/
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../devcodeicc/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.devCodeIcc = r.devCodeIcc;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'devCode': vm.q.devCode, 'proLineId': vm.q.proLineId},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                devCode: '',
                proLineId: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getTreeList: function() {
            Ajax.request({
                url: '../sys/user/treeDeptByType/DT_CJKSCX',
                async: false,
                successCallback: function (r) {
                    vm.treeList = r.list;
                }
            });
        },
        //加载列表页树
        getStore: function () {
            deptZtree = $.fn.zTree.init($("#storeTree"), deptSetting, vm.treeList);
            deptZtree.expandAll(true);
        },
        getTree: function () {
            //加载分类树
            ztree = $.fn.zTree.init($("#categoryTree"), setting, vm.treeList);

            var node = ztree.getNodeByParam("deptId", vm.devCodeIcc.proLineId);
            if (node) {
                vm.devCodeIcc.proLineName = node.name;
                ztree.selectNode(node);
                ztree.expandNode(node);
            } else {
                node = ztree.getNodeByParam("deptId", "01");
                ztree.selectNode(node);
                ztree.expandAll(true);
            }
        },
        deptTree: function () {
            openWindow({
                title: "选择生产线",
                area: ['300px', '450px'],
                content: jQuery("#categoryLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    if (node[0].deptType === 'DT_CJKSCX') {
                        // //选择上级菜单
                        vm.devCodeIcc.proLineId = node[0].deptId;
                        vm.devCodeIcc.proLineName = node[0].name;
                        layer.close(index);
                    }
                }
            });
        }
    }
});
